package georeduy.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.Query;

import georeduy.utilidades.*;
import georeduy.clases.*;
import georeduy.dao.interfaces.IUsuarioDAO;

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class UsuarioDAO implements IUsuarioDAO {
	
	
	@javax.persistence.PersistenceContext(unitName="Geored.uy-JPA" )
	private javax.persistence.EntityManager em;

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public Usuario insert(Usuario entity )
	{try
		{ em.persist(entity );	return entity;
		}
		catch (Throwable ex)
		{ throw ExceptionManager.process(ex);}
	}

	public void update(Usuario entity)
	{
		em.merge(entity);
	}
	
	 

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void delete(Usuario entity )
	{try
		{	Usuario managedEntity = em.find(Usuario.class ,entity.getId() );
			em.remove(managedEntity);
		}
		catch (Throwable ex)
		{	throw ExceptionManager.process(ex); }

	}
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void delete(Integer id )
	{	try
		{	Usuario managedEntity = em.find(Usuario.class ,id );
			em.remove(managedEntity);
		}catch (Throwable ex)
		{throw ExceptionManager.process(ex);}
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public Usuario findById(Integer id )
	{ try
		{ Usuario managedEntity = em.find(Usuario.class ,id );
			return managedEntity;
		}catch (Throwable ex)
		{throw ExceptionManager.process(ex);}
     }

	@SuppressWarnings("unchecked")
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public List<Usuario> findAll()
	{	try
		{	javax.persistence.Query query = em.createQuery("select o from Usuario o" );
			return query.getResultList();
		}
		catch (Throwable ex)
		{ throw ExceptionManager.process(ex);	}

	}
/*
	@SuppressWarnings("unchecked")
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public List<Usuario> buscarUsuarioPorLoginPassword(String login ,String password )
	{ String BUSCAR_USUARIOS_POR_LOGIN_PASSWORD = "select u from Usuario u where u.login = :login  and u.password = :password";
		try
		{
			javax.persistence.Query query = em.createQuery(BUSCAR_USUARIOS_POR_LOGIN_PASSWORD);
			query.setParameter("login" ,login );
			query.setParameter("password" ,password );
			return query.getResultList();
		}
		catch (Throwable ex)
		{
			throw ExceptionManager.process(ex);
		}

	}*/
	
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public boolean buscarUsuarioPorNick(String login)
	{ String BUSCAR_USUARIOS_POR_NICK = "select u from Usuario u where u.nick = :login";
	
			Query query = em.createQuery(BUSCAR_USUARIOS_POR_NICK);
			query.setParameter("login" ,login );
			return ((query.getResultList() != null) && (query.getResultList().size() > 0)); 
	

	}

	@Override
	public boolean LoginUsuario(String nick, String pass) {
	
			System.out.println("ENTRADA");
            String LOGIN = "select u from Usuario u where u.nick = :usuario  and u.pass = :password";
            Query query = em.createQuery(LOGIN);
			query.setParameter("usuario", nick);
			query.setParameter("password", pass);
			return ((query.getResultList() != null) && (query.getResultList().size() > 0));  		
  	
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public Usuario findByName(String nick)
	{
		Query q = em.createQuery("SELECT u FROM Usuario u WHERE u.nick = :nick");
		q.setParameter("nick", nick);
		return (Usuario) q.getSingleResult();
    }

	@Override
	public boolean ifamigo(String nick, String amigo) {
		//Query q = em.createQuery("SELECT u FROM Usuario u WHERE u.nick = :nick");
		
	//	Usuario user  = findByName(nick);
		
		boolean retorno = false;
		Query query = em.createQuery("SELECT o.contactos FROM Usuario o WHERE o.nick= :nick" );
		query.setParameter("nick", nick);
		List<Usuario> a = query.getResultList();
		if (query.getResultList() != null){
		for (Usuario usuario : a) {
			if (usuario.getNick().equals(amigo)) {
					retorno = true;}
	      	}
		}
		return retorno;
		
		
		
		
	}
}